package com.bobi.tfca.ui.mainnavigation.home.culture

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ImageView
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.PageElementIdConstant
import com.bobi.tfca.databinding.FragmentCultureBinding
import com.bobi.tfca.network.response.ArticleResponse
import com.bobi.tfca.ui.adapter.ArticleAdapter

class CultureFragment: BaseFragment<FragmentCultureBinding, CultureFragmentVM>(
    FragmentCultureBinding::inflate,
    CultureFragmentVM::class.java,
    true
) {
    private lateinit var articleRV: RecyclerView
    private lateinit var noDataIV: ImageView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentCultureBinding,
        viewModel: CultureFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 初始化RV
        initRecyclerView(binding)

        // 设置可见性的publicViewModel

        noDataIV = binding.ivNoData

        // 设置跳转写文章
        binding.ivAddNew.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigate(R.id.action_mainNavigationFragment_to_writeArticleFragment)
        }

        // 设置activities监听
        viewModel!!.articles.observe(this@CultureFragment) {
            val adapter = articleRV.adapter as ArticleAdapter
            adapter.setItemList(it)
            adapter.notifyDataSetChanged()
        }

        // 设置菜单为监听的
        publicViewModel!!.apply {
            visiblePageElements.observe(this@CultureFragment) {
                binding.ivAddNew.visibility = getElementVisibility(PageElementIdConstant.FRAGMENT_CULTURE_IV_ADD_NEW)
            }
        }
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentCultureBinding
    ) {
        // 获取RecyclerView实例
        articleRV = binding.rvArticle

        // 获得所有文章
        val articleList : ArrayList<ArticleResponse> = arrayListOf()
        // 设置布局管理器
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        articleRV.layoutManager = linearLayoutManager
        articleRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        var articleAdapter = ArticleAdapter(this, articleList)

        // 设置群聊天室监听器
        articleAdapter.setOnItemClickListener(object : ArticleAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 设置跳转详情
                publicViewModel!!.article.value = articleAdapter.articleList[position]
                requireActivity().findNavController(R.id.app_nav)
                    .navigate(R.id.action_mainNavigationFragment_to_showArticleFragment)
            }
        })
        // 绑定Adapter
        articleRV.adapter = articleAdapter
    }

    override fun resumeFragment(viewModel: CultureFragmentVM?) {
        super.resumeFragment(viewModel)
        viewModel!!.findAllArticles(publicViewModel!!, this, noDataIV)
    }
}